﻿using BaiTu.ConfigFileManager;
using BaiTu.ConfigInfo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaiTu.Config
{
    public class EventBusConfig:IConfig
    {
         private static System.Timers.Timer redisConfigTimer = new System.Timers.Timer(Framework.Core.Global.BaiTuGlobal.ConfigTimer);

        private static EventBusConfigInfo m_configInfo;

        static EventBusConfig()
        {
            m_configInfo = BaiTu.ConfigFileManager.EventBusConfigFileManager.LoadConfig();
            redisConfigTimer.AutoReset = true;
            redisConfigTimer.Enabled = true;
            redisConfigTimer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed);
            redisConfigTimer.Start();
        }
        private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            ResetConfig();
        }
        /// <summary>
        /// 重设配置类实例
        /// </summary>
        public static void ResetConfig()
        {
            m_configInfo = EventBusConfigFileManager.LoadConfig();
        }
        /// <summary>
        /// 获取配置类实例
        /// </summary>
        /// <returns></returns>
        public static EventBusConfigInfo GetConfig()
        {
            if (m_configInfo == null)
            {
                m_configInfo = BaiTu.ConfigFileManager.EventBusConfigFileManager.LoadConfig();
            }
            return m_configInfo;
        }
        /// <summary>
        /// 保存配置类实例
        /// </summary>
        /// <param name="configinfo"></param>
        /// <returns></returns>
        public static bool SaveConfig(EventBusConfigInfo configinfo)
        {
            EventBusConfigFileManager rcfm = new EventBusConfigFileManager();
            EventBusConfigFileManager.ConfigInfo = configinfo;
            return rcfm.SaveConfig();
        }

        public void SaveConfigInitialize()
        {
            if (GetConfig() == null)
            {
                SaveConfig(new EventBusConfigInfo());
            }
        }
    }
}
